﻿//***********************************************************************************
// 文件名称：Form1
// 功能描述：windows窗体
// 数据表：
// 作者：liuxiaohu
// 日期：2017/3/13 9:20:30
// 修改记录：
//***********************************************************************************

using System;
using System.Windows.Forms;

namespace GameClient
{
    /// <summary>
    /// 窗体类
    /// </summary>
    public partial class Form1 : Form
    {
        /// <summary>
        /// 窗体初始化
        /// </summary>
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 获取排行榜信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void getRankBtn_Click(object sender, EventArgs e)
        {
            dgvBox.Rows.Clear();

            Int32 playerId = string.IsNullOrEmpty(player_snTB.Text) ? 0 : Convert.ToInt32(player_snTB.Text);
            try
            {
                RankInfo info = HttpWebHelper.GetScoreRank(playerId);
                if (info != null)
                {
                    scoreTB.Text = info.PlayerScore.ToString();

                    // 将排行榜数据显示到datagridview控件中
                    foreach (Rank rank in info.RankList)
                    {
                        Int32 index = dgvBox.Rows.Add();
                        dgvBox.Rows[index].Cells[0].Value = rank.rankNum.ToString();
                        dgvBox.Rows[index].Cells[1].Value = rank.playerName;
                        dgvBox.Rows[index].Cells[2].Value = rank.playerScores.ToString();
                    }

                    // 对datagridview中的数据按照玩家排名进行排序
                    dgvBox.Sort(dgvBox.Columns[0], System.ComponentModel.ListSortDirection.Ascending);
                }
                else
                {
                    scoreTB.Text = "";
                }
            }
            catch (Exception e1)
            {
                errorMTB.Text = e1.Message;
            }
        }

        /// <summary>
        /// 使用物品
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void useItemBtn_Click(object sender, EventArgs e)
        {
            Int32 playerId = String.IsNullOrEmpty(player_snTB1.Text) ? 0 : Convert.ToInt32(player_snTB1.Text);
            Int32 itemId = String.IsNullOrEmpty(player_snTB1.Text) ? 0 : Convert.ToInt32(itemIdTB.Text);
            Int32 itemNum = String.IsNullOrEmpty(player_snTB1.Text) ? 0 : Convert.ToInt32(itemNumTB.Text);

            resultTB.Text = HttpWebHelper.UseItem(playerId, itemId, itemNum) == "true" ? "使用成功" : "使用失败";
        }
    }
}
